package my.jieh.tips.swing;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ListWrap
    extends JFrame {

  static final String asCity[] = {
      "Asmara", "Berlin", "Kuala Lumpur",
      "Paramaribo", "Paris", "Sana",
      "Sucre", "Tokyo", "Ulan Bator",
      "Valletta", "Washington", "Yaounde"
  };

  static final String asCountry[] = {
      "Eritrea", "Germany", "Malaysia",
      "Suriname", "France", "Yemen",
      "Bolivia", "Japan", "Mongolia",
      "Malta", "US", "Cameroon"
  };

  int current;
  JTextField input;
  JLabel where;
  JButton submitButton, nextButton;

  public ListWrap() {
    setTitle("Capitals");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel westPanel = new JPanel(
        new GridLayout(3, 1));
    westPanel.add(new JLabel(
        "What is the capitol of...",
        JLabel.CENTER));
    westPanel.add(where =
                  new JLabel(asCountry[0], JLabel.CENTER));
    input = new JTextField(10);
    JPanel westSouth = new JPanel();
    westSouth.add(input);
    westPanel.add(westSouth);

    ListSelectionListener selectionListener =
        new ListSelectionListener() {
      public void valueChanged(
          ListSelectionEvent lse) {
        if (!lse.getValueIsAdjusting()) {
          Object source = lse.getSource();
          int index = (
              (JList) source).getSelectedIndex();
          input.setText(asCity[index]);
          checkAnswer();
        }
      }
    };

    JTabbedPane tabbedPane = new JTabbedPane();

    JList normalWrap = new JList(asCity);
    normalWrap.addListSelectionListener(
        selectionListener);
    tabbedPane.addTab("Standard", null,
                      new JScrollPane(
        normalWrap), "Standard Orientation");

    JList verticalWrap = new JList(asCity);
    verticalWrap.setLayoutOrientation(
        JList.VERTICAL_WRAP);
    verticalWrap.addListSelectionListener(
        selectionListener);
    tabbedPane.addTab("Vertical Wrap", null,
                      verticalWrap, "Vertical Wrap Orientation");

    JList horizontalWrap = new JList(asCity);
    horizontalWrap.setLayoutOrientation(
        JList.HORIZONTAL_WRAP);
    horizontalWrap.addListSelectionListener(
        selectionListener);
    tabbedPane.addTab("Horizontal Wrap", null,
                      horizontalWrap, "Horizontal Wrap Orientation");

    JPanel eastPanel = new JPanel();
    eastPanel.add(tabbedPane);

    ActionListener actionListener =
        new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if (source == submitButton) {
          checkAnswer();
        }
        else if (source == nextButton) {
          doNext();
        }
      }
    };

    JPanel southPanel = new JPanel();

    submitButton = new JButton("Submit Answer");
    submitButton.addActionListener(actionListener);
    southPanel.add(submitButton);

    nextButton = new JButton("Next");
    nextButton.addActionListener(actionListener);
    southPanel.add(nextButton);

    Container contentPane = getContentPane();
    contentPane.add(westPanel, BorderLayout.WEST);
    contentPane.add(eastPanel, BorderLayout.EAST);
    contentPane.add(southPanel, BorderLayout.SOUTH);

    pack();
    show();
  }

  public void checkAnswer() {
    int messageType = 0;
    String result = null;
    String s = input.getText();

    if (s.equalsIgnoreCase(asCity[current])) {
      result = "Correct!";
      messageType = JOptionPane.INFORMATION_MESSAGE;
    }
    else {
      result = "Try Again.";
      messageType = JOptionPane.WARNING_MESSAGE;
    }

    JOptionPane.showMessageDialog(this,
                                  result, "Result", messageType);
  }

  public void doNext() {
    current++;
    if (current >= asCountry.length) {
      current = 0;
    }
    where.setText(asCountry[current]);
    input.setText("");
  }

  public static void main(String args[]) {
    new ListWrap();
  }
}